//
//  CALayer+hfAnimation.m
//  SecondPackage
//
//  Created by 韩艳锋 on 2019/6/28.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "CALayer+hfAnimation.h"

@implementation CALayer (hfAnimation)
- (void)addAnimationOne{
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = 0.25f;
    //设置运动type
    animation.type = @"cube";
    animation.subtype = kCATransitionFromRight;
    //设置运动速度
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self addAnimation:animation forKey:@"animation"];
}

- (void)addAnimationTow {
    CALayer * shapLayer = [[CALayer alloc] init];
//    shapLayer.frame = self.bounds;
    shapLayer.backgroundColor = [UIColor blackColor].CGColor;
//    shapLayer.anchorPoint = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    /// 起始半径
    CGFloat star = 50;
    /// 终止半径
    CGFloat end = sqrtf((self.bounds.size.width) * (self.bounds.size.width) + (self.bounds.size.height) * (self.bounds.size.height)) / 2;
    shapLayer.frame = CGRectMake(self.bounds.size.width / 2 - star, self.bounds.size.height / 2 - star, star*2, star*2);
    shapLayer.cornerRadius = 50;
    self.mask = shapLayer;
    CFTimeInterval time = 0.25;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.duration = time;
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(end/star, end/star, 1)];
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [shapLayer addAnimation:animation forKey:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((time + 0.05) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.mask = nil;
    });
}

- (void)addAnimationThree {
    [self addAnimation:[[CATransition alloc] init] forKey:nil];
}
@end
